package interview.juc;

/**
 * @Author: zhangrongqing
 * @Date: 2020/5/22 9:48
 */
public class PrintAAndB3 {
    int num = 1;

    public void printAandB(){

        synchronized (this){
            while (true){
                this.notify();
                if(num <= 100){
                    System.out.println(Thread.currentThread().getName()+",num is"+ ++num);
                }else {
                    break;
                }

                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


        }
    }

    public static void main(String[] args) {
        PrintAAndB3 task = new PrintAAndB3();
        Thread thread = new Thread(() -> task.printAandB());
        thread.setName("A");
        thread.start();

        Thread thread2 = new Thread(() -> task.printAandB());
        thread2.setName("B");
        thread2.start();
    }
}
